<template>
  <div class="container-cc">
    <div class="box" v-if="loadFlag">
      <img class="mess-image" :src="coverImage" alt="">
      <div class="title">{{ title }}</div>
      <!-- <div class="item-msg">
            <div class="author">{{ issue }}</div>
            <div class="time">{{ issueTime }}</div>
        </div> -->
      <div class="score">{{ giftScore }}
        <span class="jfen">积分</span>
      </div>
      <div class="detail">
        <div class="spxq">—— 商品详情 ——</div>
        <div class="intro">{{ intro }}</div>
        <img class="mess-image" :src="detailImage" alt="">
      </div>
      <div class="dhbtn" @click="exchange(giftId)">

        <van-button round block type="primary" native-type="submit">
          兑换
        </van-button>
      </div>
    </div>
    <div class="loading-container" v-if="loadingiconFlag">
      <van-loading color="#1989fa" type="spinner">加载中...</van-loading>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { reactive } from 'vue';
import { useRouter } from 'vue-router';
import { onMounted } from 'vue'
import { getPointDetail } from '@/api/message';
import { isFirstLogin } from '@/api/login';
import { showToast, showDialog } from 'vant';
const router = useRouter()
const gift = ref({})
const issue = ref('')
const issueTime = ref('')
const title = ref('')
const coverImage = ref('')
const detailImage = ref('')
const giftScore = ref('')
const giftId = ref('')
const intro = ref('')
const loadFlag = ref(false);
const loadingiconFlag = ref(false);
const scoreLimit = localStorage.getItem('scoreLowerLimit')
console.log('scoreLimit:', scoreLimit);
const existingScore = ref('');
const openId = localStorage.getItem('openId');
const mobilePhone = ref(JSON.parse(localStorage.getItem('userInfo'))[0].mobilePhone);

const getUserInfo = () => {
  const params = {
    openId: openId,
    mobilePhone: mobilePhone.value,
  }
  isFirstLogin(params).then((res) => {
    if (res.code === 200) {
      if (res.data.length > 0) {
        const { score } = res.data[0]
        existingScore.value = score
      }
    } else {
      showToast(res.msg)
    }
  }).catch((err) => {
    console.log(err)
  })
}


const getDetail = () => {
  loadingiconFlag.value = true
  getPointDetail(router.currentRoute.value.query.id).then((res) => {
    if (res.code === 200) {
      gift.value = res.data
      issue.value = gift.value.issue
      title.value = gift.value.title
      issueTime.value = gift.value.issueTime
      intro.value = gift.value.intro
      giftScore.value = gift.value.score
      giftId.value = gift.value.giftId
      if (JSON.parse(gift.value.coverImage).length > 0) {
        coverImage.value = process.env.VITE_API_API_IMAGE_URL + JSON.parse(gift.value.coverImage)[0].url
      }
      if (JSON.parse(gift.value.detailImage).length > 0) {
        detailImage.value = process.env.VITE_API_API_IMAGE_URL + JSON.parse(gift.value.detailImage)[0].url
      }
      loadFlag.value = true
      loadingiconFlag.value = false
    } else {
      showToast(res.msg)
      loadFlag.value = true
      loadingiconFlag.value = false
    }
  }).catch((err) => {
    console.log(err)
  })
}

const exchange = (giftId) => {
  console.log('giftId:', giftId);
  console.log('existingScore.value:', existingScore.value);
  console.log('giftScore.value:', giftScore.value);
  if (existingScore.value < scoreLimit) {
    showDialog({
      message: `个人积分达到${scoreLimit}才能兑换礼品，请继续学习获取积分`,
      theme: 'round-button',
      confirmButtonColor: '#1989fa'
    })
    return
  }
  if (existingScore.value < giftScore.value) {
    showDialog({
      message: '兑换积分不足，请重新选择礼品',
      theme: 'round-button',
      confirmButtonColor: '#1989fa'
    })
    return
  } else {
    router.push({ path: '/ConfirmOrder', query: { giftId: giftId } })
  }
}

onMounted(() => {
  getDetail()
  getUserInfo()
})
</script>


<style lang="scss" scoped>
.container-cc {
  background: #fff;
  overflow: hidden;
  box-sizing: border-box;
  margin-bottom: 1rem;
  padding: 1rem 1.4rem;
}

.ellipsis-multiline {
  word-break: break-all;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}

.title {
  font-size: 1.6rem;
  // text-align: center;
  // font-weight: bold;
}

.item-msg {
  font-size: 1.2rem;
  padding: 1rem 0;
}

.author {
  margin-bottom: 1rem;
}

.time {
  color: #ccc;
}

.newscontent {
  font-size: 1.5rem;
  text-indent: 2ch;
  line-height: 2.5rem;
  padding: 1rem 0;
}

.mess-image {
  width: 100%;
  margin-top: 1rem;
}

.index-image {
  width: 12rem;
  height: 9rem;
  margin-right: 2rem;
  border-radius: .8rem;
}

.index-title {
  font-size: 1.4rem;
  margin-bottom: 5rem;
}

.index-msg {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: .6rem;
  color: #aaa;
  font-size: 1.2rem;
}

.index-time {
  margin-top: 0.2rem;
}

.index-box {
  display: flex;
  justify-content: flex-start;
  margin-bottom: 1rem;
  margin-top: 2rem;
  border-top: 1px solid #ccc;
  border-radius: 1rem;
  box-sizing: border-box;
  background: #fff;
  padding: 2rem 1.4rem 1rem;
}

.box-right {
  width: 20rem;
}

.box {
  // padding: 1rem 1.4rem;
  border-radius: 1rem;
}

.wxpDialogContentClass {
  padding: 10px 8px;
  word-wrap: break-word;
  font-size: 14px;
  white-space: pre-wrap;
  color: #646566;
  max-height: 66vh;
  line-height: 20px;
  overflow-y: hidden;
  -ms-overflow-style: none;
  scrollbar-width: none;
}

.wxpDialogContentClass::-webkit-scrollbar {
  display: none;
  /* 针对WebKit浏览器 */
  width: 0;
  height: 0;
}

.wxContent {
  display: block;
  max-height: 66vh;
  overflow: auto;
}

::v-deep img {
  max-width: 100%;
}

.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 80vh;
}

.score {
  color: red;
  font-size: 1.8rem;
}

.jfen {
  font-size: 1.2rem;
  color: #999;
}

.spxq {
  text-align: center;
  font-size: 1.6rem;
}

.intro {
  text-indent: 2ch;
  font-size: 1.6rem;
  margin-top: 2rem;
  margin-bottom: 1rem;
}

.detail {
  padding-bottom: 5rem;
  margin-top: 2rem;
}

.dhbtn {
  position: fixed;
  bottom: 1rem;
  width: 90%;
  left: 5%;
}
</style>